package com.whnk.pm.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import com.whnk.pm.domain.*;
import com.whnk.pm.mapper.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.whnk.common.support.Convert;
import com.whnk.pm.dto.SignSyncDTO;
import com.whnk.pm.service.ISignInfoService;

/**
 * 项目考勤 服务层实现
 *
 * @author whwhnk
 * @date 2019-07-12
 */
@Service
public class SignInfoServiceImpl implements ISignInfoService {
    @Autowired
    private SignInfoMapper signInfoMapper;

    @Autowired
    private ImpSignInfoMapper impSignInfoMapper;

    @Autowired
    private CompInfoMapper compInfoMapper;

    @Autowired
    private BaseInfoMapper baseInfoMapper;

    @Autowired
    private ProInfoMapper proInfoMapper;

    /**
     * 查询项目考勤信息
     *
     * @param signId 项目考勤ID
     * @return 项目考勤信息
     */
    @Override
    public SignInfo selectSignInfoById(Integer signId) {
        return signInfoMapper.selectSignInfoById(signId);
    }

    /**
     * 查询项目考勤列表
     *
     * @param signInfo 项目考勤信息
     * @return 项目考勤集合
     */
    @Override
    public List<SignInfo> selectSignInfoList(SignInfo signInfo) {
        return signInfoMapper.selectSignInfoList(signInfo);
    }

    /**
     * 新增项目考勤
     *
     * @param signInfo 项目考勤信息
     * @return 结果
     */
    @Override
    public int insertSignInfo(SignInfo signInfo) {
        return signInfoMapper.insertSignInfo(signInfo);
    }

    /**
     * 修改项目考勤
     *
     * @param signInfo 项目考勤信息
     * @return 结果
     */
    @Override
    public int updateSignInfo(SignInfo signInfo) {
        return signInfoMapper.updateSignInfo(signInfo);
    }

    /**
     * 删除项目考勤对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSignInfoByIds(String ids) {
        return signInfoMapper.deleteSignInfoByIds(Convert.toStrArray(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncSignInfo(SignSyncDTO signSyncDTO) {
        List<ImpSignInfo> impSignInfoList = impSignInfoMapper.getBySignDate(signSyncDTO.getBeginTime(), signSyncDTO.getEndTime());
        for (ImpSignInfo impSignInfo : impSignInfoList) {
            SignInfo signInfo = new SignInfo();
            BeanUtils.copyProperties(impSignInfo, signInfo);

            SignInfo selectSignInfo = new SignInfo();
            selectSignInfo.setEmpId(impSignInfo.getEmpId());
            selectSignInfo.setSignDt(impSignInfo.getSignDt());

            CompInfo compInfo = compInfoMapper.selectCompInfoByCompName(impSignInfo.getEmpComp());
            if (compInfo != null) {
                signInfo.setCompId(compInfo.getCompId());
                signInfo.setCompNm(compInfo.getCompNm());
                signInfo.setCompSn(compInfo.getCompSn());
            }
            List<SignInfo> signInfoList = signInfoMapper.selectSignInfoList(selectSignInfo);
            if (signInfoList.size() > 0) {
                signInfo.setSignId(signInfoList.get(0).getSignId());
                signInfoMapper.updateSignInfo(signInfo);
            } else {
                signInfo.setSignId(null);
                signInfoMapper.insertSignInfo(signInfo);
            }
        }
    }

    @Override
    public void manualSign(String empId, String proName) throws ParseException {
        SignInfo signInfo = baseSignInfoData(empId, proName);

        Date date = new Date();
        signInfo.setSignDt(date);

        SignInfo selectSignInfo = new SignInfo();
        selectSignInfo.setEmpId(empId);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String DateStr = dateFormat.format(date);
        Date signDate = dateFormat.parse(DateStr);
        selectSignInfo.setSignDt(signDate);
        List<SignInfo> signInfoList = signInfoMapper.selectSignInfoList(selectSignInfo);
        if (signInfoList.size() > 0) {
            SignInfo signInfoData = signInfoList.get(0);
            if (signInfoData.getSignDt().before(date)) {
                return;
            }
            signInfo.setSignId(signInfoData.getSignId());
            signInfoMapper.updateSignInfo(signInfo);
        } else {
            signInfo.setSignId(null);
            signInfo.setSignStti(date);
            signInfoMapper.insertSignInfo(signInfo);
        }
    }

    @Override
    public void manualSignOut(String empId, String proName) throws ParseException {
        SignInfo signInfo = baseSignInfoData(empId, proName);

        Date date = new Date();
        signInfo.setSignDt(date);

        SignInfo selectSignInfo = new SignInfo();
        selectSignInfo.setEmpId(empId);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String DateStr = dateFormat.format(date);
        Date signDate = dateFormat.parse(DateStr);
        selectSignInfo.setSignDt(signDate);
        List<SignInfo> signInfoList = signInfoMapper.selectSignInfoList(selectSignInfo);
        if (signInfoList.size() > 0) {
            SignInfo signInfoData = signInfoList.get(0);
            signInfo.setSignId(signInfoData.getSignId());
            signInfo.setSignEdti(date);
            signInfoMapper.updateSignInfo(signInfo);
        } else {
            signInfo.setSignId(null);
            signInfo.setSignEdti(date);
            signInfoMapper.insertSignInfo(signInfo);
        }
    }

    private SignInfo baseSignInfoData(String empId, String proName) {
        SignInfo signInfo = new SignInfo();
        BaseInfo baseInfo = baseInfoMapper.selectBaseInfoById(empId);
        if (baseInfo != null) {
            signInfo.setEmpId(baseInfo.getEmpId());
            signInfo.setEmpNm(baseInfo.getEmpNm());
        }
        String companyName = baseInfo.getEmpComp();
        /**
         * 查询项目信息
         */
        ProInfo proInfo = new ProInfo();
        proInfo.setProName(proName);
        ProInfo proInfoData = proInfoMapper.selectProInfoList(proInfo).get(0);
        if (proInfoData != null) {
            signInfo.setProId(String.valueOf(proInfoData.getProId()));
            signInfo.setProName(proInfoData.getProName());
            signInfo.setProSn(proInfoData.getProSn());
        }
        /**
         * 查询公司信息
         */
        CompInfo compInfo = compInfoMapper.selectCompInfoByCompName(companyName);
        if (compInfo != null) {
            signInfo.setCompId(compInfo.getCompId());
            signInfo.setCompNm(compInfo.getCompNm());
            signInfo.setCompSn(compInfo.getCompSn());
        }
        return signInfo;
    }
}
